package com.yyge.controller;

import cn.hutool.core.lang.UUID;
import com.wf.captcha.GifCaptcha;
import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.base.Captcha;
import com.yyge.context.RedisContext;
import com.yyge.dto.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Api("登陆控制器") //swagger
@RestController
public class CaptchaController {
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RedisContext redisContext;

    @ApiOperation("图片验证码")
    @GetMapping("/captcha")
    public R captcha(){
        /*导入com.w.....的captcha*/
        Captcha captcha=new GifCaptcha(130,48);
        captcha.setLen(4);

        //获取验证码文本
        String text=captcha.text();

        //img url
        String base64=captcha.toBase64();

        //生成UUID
        String uuid= UUID.fastUUID().toString();

        //将UUID放入redis(过期时间5分钟)
        redisTemplate.opsForValue().set(
         redisContext.getCaptchaKey(uuid),text,5, TimeUnit.MINUTES);

        //将返回内容放入map集合
        Map map=new HashMap<>();
        map.put("uuid",uuid);
        map.put("image",base64);
        return R.ok(map);
    }
}
